package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MyCostMapper;
import com.ruoyi.system.domain.MyCost;
import com.ruoyi.system.service.IMyCostService;

import javax.validation.Validator;

/**
 * 项目欠费统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-15
 */
@Service
public class MyCostServiceImpl implements IMyCostService 
{
    @Autowired
    private MyCostMapper myCostMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询项目欠费统计
     * 
     * @param id 项目欠费统计主键
     * @return 项目欠费统计
     */
    @Override
    public MyCost selectMyCostById(String id)
    {
        return myCostMapper.selectMyCostById(id);
    }

    /**
     * 查询项目欠费统计列表
     * 
     * @param myCost 项目欠费统计
     * @return 项目欠费统计
     */
    @Override
    public List<MyCost> selectMyCostList(MyCost myCost)
    {
        return myCostMapper.selectMyCostList(myCost);
    }

    /**
     * 新增项目欠费统计
     * 
     * @param myCost 项目欠费统计
     * @return 结果
     */
    @Override
    public int insertMyCost(MyCost myCost)
    {
        return myCostMapper.insertMyCost(myCost);
    }

    /**
     * 修改项目欠费统计
     * 
     * @param myCost 项目欠费统计
     * @return 结果
     */
    @Override
    public int updateMyCost(MyCost myCost)
    {
        return myCostMapper.updateMyCost(myCost);
    }

    /**
     * 批量删除项目欠费统计
     * 
     * @param ids 需要删除的项目欠费统计主键
     * @return 结果
     */
    @Override
    public int deleteMyCostByIds(String[] ids)
    {
        return myCostMapper.deleteMyCostByIds(ids);
    }

    /**
     * 删除项目欠费统计信息
     * 
     * @param id 项目欠费统计主键
     * @return 结果
     */
    @Override
    public int deleteMyCostById(String id)
    {
        return myCostMapper.deleteMyCostById(id);
    }

    @Override
    public String importCost(List<MyCost> myCost, String operName) {
        if (StringUtils.isNull(myCost) || myCost.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (MyCost myCosti : myCost)
        {
            try
            {
                // 验证是否存在这个用户
                MyCost u = myCostMapper.selectMyCostById(myCosti.getId());
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, myCosti);
                    myCostMapper.insertMyCost(myCosti);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + myCosti.getProjectname() + " 导入成功");
                }
                else
                {
                    BeanValidators.validateWithException(validator, myCosti);

                    myCostMapper.updateMyCost(myCosti);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + myCosti.getProjectname() + " 更新成功");
                }
                /*else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }*/
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + myCosti.getProjectname() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
